﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace Crystal.CLL
{
    public static class ContentManager
    {
        #region Member Variable
        private static Dictionary<string, string> _currentContent = null;

        #endregion

        #region Constructors
        static ContentManager()
        {
            LoadContentFromXml();
        }
        #endregion

        #region Methods
        /// <summary>
        /// This Function is used to Reload Message From XML File it Check that Hashtable object Current is null
        /// then it Reload message from Xml Files
        /// </summary>
        public static void ReLoadContentFromXml()
        {
            _currentContent = null;

            LoadContentFromXml();
        }

        /// <summary>
        /// This Function is used to load Message From XML File it Check that Hashtable object Current is null
        /// then it Load message from Xml Files Otherwise it it doesnot Load message
        /// </summary>
        private static void LoadContentFromXml()
        {
            if (_currentContent == null)
            {
                DataSet dsContent = new DataSet();

                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                dsContent.ReadXml(_assembly.GetManifestResourceStream("Crystal.CLL.Components.EntityFiles.Content.xml"));
                _currentContent = new Dictionary<string, string>();

                if (dsContent.Tables.Count != 0)
                {
                    DataTable dtContent = dsContent.Tables[0];
                    if (dtContent.Rows.Count != 0)
                    {
                        foreach (DataRow drTitle in dtContent.Rows)
                        {
                            _currentContent.Add(Convert.ToString(drTitle[0]), Convert.ToString(drTitle[1]));
                        }
                    }
                    dtContent = null;
                }
                dsContent = null;
            }
        }

        /// <summary>
        /// Get The Message from Message Hashtable and format it with the argumaents passed.
        /// </summary>
        /// <param name="strMessageName">string</param>
        /// <param name="args">param object[]</param>       
        /// <returns>string</returns>
        public static string GetContent(string pageURL)
        {
            return (_currentContent.ContainsKey(pageURL) == true ? Convert.ToString(_currentContent[pageURL]) : Convert.ToString(_currentContent[""]));
        }
        #endregion
    }
}
